package com.gxa.controller;

import com.gxa.exception.CustomerException;
import com.gxa.pojo.Patient;
import com.gxa.service.PatientService;
import com.gxa.util.JsonResult;
import com.gxa.util.QueryInfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/09/23
 * Description
 * Version: V1.0
 */

@Controller
@RequestMapping("/patient")
@Transactional
public class PatientController {

    @Resource
    private PatientService patientService = null;
    JsonResult jsonResult = new JsonResult();

    /**
     * 登录接口
     *
     * @param map 用户名和密码
     * @return 返回登录信息
     */
    @PostMapping("/login")
    @ResponseBody
    public Map login(@RequestBody Map<String, Object> map) {
        try {
            Patient patient = patientService.login(map);
            if (patient == null) {
                jsonResult.setCode("500");
                jsonResult.setMsg("登录失败，用户名或密码错误");
            } else {
                jsonResult.setData(patient);
                jsonResult.setCode("200");
                jsonResult.setMsg("登录成功");
            }
        } catch (CustomerException c) {
            jsonResult.setMsg(c.getMessage());
            jsonResult.setCode("501");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode("500");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    /**
     * 注册接口
     *
     * @param patient 注册的患者信息
     * @return 返回注册状态码
     * @throws CustomerException 异常处理
     */
    @PostMapping("/addPatient")
    @ResponseBody
    public Map addPatient(@RequestBody Patient patient) throws CustomerException {
        boolean register = patientService.addPatient(patient);
        if (register == true) {
            jsonResult.setData(null);
            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功");
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("注册失败");
        }
        return jsonResult.getValues();
    }

    /**
     * 查询所有患者信息
     *
     * @return 返回查询到的所有患者信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAll() {
        List<Patient> all = patientService.findAll();
        jsonResult.setData(all);
        jsonResult.setMsg("");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    /**
     * 分页查询
     *
     * @param map 分页信息
     * @return 返回分页查询结果
     */
    @PostMapping("/findByPage")
    @ResponseBody
    public Map findByPage(@RequestBody Map<String, Integer> map) {
        QueryInfo queryInfo = new QueryInfo();
        //当前页面
        queryInfo.setCurrentPage(map.get("page"));
        //分页大小
        queryInfo.setPagesize(map.get("limit"));
        List<Patient> byPage = patientService.findByPage(queryInfo);
        System.out.println(byPage);
        jsonResult.setData(byPage);
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询");
        return jsonResult.getValues();
    }

    /**
     * 通过id查询患者信息
     *
     * @param map 患者id
     * @return 返回患者信息
     */
    @PostMapping("/findById")
    @ResponseBody
    public Map findById(@RequestBody Map<String, Integer> map) {
        Patient byId = patientService.findById(map.get("id"));
        jsonResult.setData(byId);
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    /**
     * 通过医生的id查询医生关注的患者
     * @param doctorId  医生的id
     * @return  患者的id
     */
    @PostMapping("/findByDoctorId")
    @ResponseBody
    public Map findByDoctorId(@RequestBody Integer doctorId) {
        try {
            List<Patient> byDoctorId = patientService.findByDoctorId(doctorId);
            jsonResult.setCode("200");
            jsonResult.setMsg("医生关注的患者");
            jsonResult.setData(byDoctorId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode("500");
            jsonResult.setMsg("系统开小差了");
            jsonResult.setData(null);
        }
        return jsonResult.getValues();

    }

    /**
     * 添加核桃币
     *
     * @param map 核桃币数额和患者id
     * @return 返回充值信息
     */
    @PostMapping("/addAcount")
    @ResponseBody
    public Map addAcount(@RequestBody Map<String, Integer> map) {
        patientService.addAcount(map.get("acount"), map.get("id"));
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    /**
     * 扣除核桃币
     *
     * @param map 核桃币数额和患者id
     * @return 返回扣除信息
     */
    @PostMapping("/deductAcount")
    @ResponseBody
    public Map deductAcount(@RequestBody Map<String, Integer> map) {
        patientService.deductAcount(map.get("acount"), map.get("id"));
        jsonResult.setMsg("扣除");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }


}
